package com.futong.cloud.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.futong.cloud.action.ActionModel;
import com.futong.cloud.dao.entity.ActionFiled;
import com.futong.cloud.dao.entity.ActionFunction;
import com.futong.cloud.dao.entity.ActionInfo;
import com.futong.cloud.dao.entity.ActionRelation;
import com.futong.cloud.utils.SpringUtils;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @program: futong-cm
 * @description:
 * @author: lipengpeng
 * @create: 2021/10/12 15:35
 */
@Mapper
public interface ActionMapper {
    @Mapper
    interface ActionInfoMapper extends BaseMapper<ActionInfo> {
        @Select("select * from action_info where enable=1")
        List<ActionInfo> listActionInfoEnbale();
    }
    @Mapper
    interface ActionFunctionMapper extends BaseMapper<ActionFunction> {
        @Select("select * from action_function where enable=1")
        List<ActionFunction> listActionFunctionEnbale();
        @Select("SELECT a.* FROM action_function a JOIN action_relation b ON a.id = b.join_id AND b.type = 'function' where b.action_id=#{id} ORDER BY b.sort")
        List<ActionModel.ActionFunctionModel> findActionFunctionByActionId(Integer id);
    }
    @Mapper
    interface ActionFiledMapper extends BaseMapper<ActionFiled> {
        @Select("select * from action_filed where enable=1")
        List<ActionFiled> listActionFiledEnbale();
        @Select("SELECT a.* FROM action_filed a JOIN action_relation b ON a.id = b.join_id AND b.type = 'filed' where b.action_id=#{id} ORDER BY b.sort")
        List<ActionModel.ActionFiledModel> findActionFiledByActionId(Integer id);
    }
    @Mapper
    interface ActionRelationMapper extends BaseMapper<ActionRelation> {}
    class Mappers{
        public static ActionMapper action = SpringUtils.getBean(ActionMapper.class);
        public static ActionMapper.ActionInfoMapper info = SpringUtils.getBean(ActionMapper.ActionInfoMapper.class);
        public static ActionMapper.ActionFunctionMapper function = SpringUtils.getBean(ActionMapper.ActionFunctionMapper.class);
        public static ActionMapper.ActionFiledMapper filed = SpringUtils.getBean(ActionMapper.ActionFiledMapper.class);
        public static ActionMapper.ActionRelationMapper relation = SpringUtils.getBean(ActionMapper.ActionRelationMapper.class);
    }
    @Select("select * from action_info where enable=1 ")
    @Results(
        value={
            @Result(property = "fileds",column = "id",javaType = List.class,many = @Many(select="com.futong.cloud.dao.ActionMapper$ActionFiledMapper.findActionFiledByActionId")),
            @Result(property = "functions",column = "id",javaType = List.class,many = @Many(select="com.futong.cloud.dao.ActionMapper$ActionFunctionMapper.findActionFunctionByActionId"))
        }
    )
    List<ActionModel> listAction();


}
